<?php

namespace Home\Controller;

class IndexController extends HomeController
{
    //首页
    public function index()
    {
        $usernum = count(M('User')->field('id')->where(array(
            'status' => 1
        ))->select());

        $totaldeal = M('Trade')->field("sum(deal) as 'totaldealnum' , sum(price) as 'totaldealprice'")->where('order_status >= 3')->select();

        $activeuser = count(M('User')->field('id')->where('lasttime between '.time().'-86400 and ' .time().'+86400')->select());

        $buyData = M()->table('trade_adver as a')->field('a.userid,a.id,a.price,a.min_limit,a.max_limit,a.trade_type,a.pay_type,b.headimg,c.trade_num,c.user_trust,c.user_praise')->join('left join trade_user as b on a.userid = b.id left join trade_user_credit as c on a.userid=c.userid')->where('a.trade_type = 1')->limit(2)->select();

        $sellData = M()->table('trade_adver as a')->field('a.userid,a.id,a.price,a.min_limit,a.max_limit,a.trade_type,a.pay_type,b.headimg,c.trade_num,c.user_trust,c.user_praise')->join('left join trade_user as b on a.userid = b.id left join trade_user_credit as c on a.userid=c.userid')->where('a.trade_type = 2')->limit(2)->select();

        $totaldata = array_merge($buyData,$sellData);

        $data = array();

        $data['totaldealnum'] = $totaldeal[0]['totaldealnum'];

        $data['totaldealprice'] = $totaldeal[0]['totaldealprice'];

        $data['data'] = $totaldata;

        $data['usernum'] = $usernum;

        $data['activeuser'] = $activeuser;

        $this->ajaxReturn($data , 'JSON');
    }

    public function btcnum($userid = NULL){
        if ($userid) {
            $btcnum = M('UserCoin')->field('btc,btcd')->where(array(
                'userid' => $userid
            ))->find();

            $btctotal = $btcnum['btc'] + $btcnum['btcd'];

            $arr = array();

            $arr['btc'] = $btcnum['btc'] + 0;

            $arr['btcd'] = $btcnum['btcd'] + 0;

            $arr['btctotal'] = $btctotal;

            $arr['type'] = 1;

            $this->ajaxReturn($arr , 'JSON');
        }
    }

    public function chatLog($userid = NULL){
        $chatLog = M('Chat')->where(array(
            'chatid' => $userid,
            'status' => 0
        ))->order('addtime asc')->select();

        $data = array();

        if ($chatLog) {
            $data['chatlog'] = 1;
        }else{
            $data['chatlog'] = 0;
        }

        $this->ajaxReturn($data , 'JSON');
    }
}